package com.socket.server;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

import com.socket.server.RequestQueue.Request;
import com.socket.server.config.Configuration;
import com.socket.server.service.ServiceThread;
import com.socket.util.Constants;

/**
 * �����̣߳�������������߳�ִ��service����������Thread���ʼ��ӵ��÷�����ʱ
 * @author luoweiyi
 *
 */
public class TaskThread extends Thread{
	
	private Socket socket;
	
	private Request request;
	
	private Configuration configuration;
	
	public TaskThread(Configuration configuration){
		this.configuration = configuration;
	}
	
	/**
	 * ����������л�ȡ����
	 */
	private void prepare(){
		this.request = RequestQueue.getInstance().getRequest();
		this.socket = request.getSocket();
	}
	
	private String getServiceImpl(String serviceApi){
		return configuration.getServiceImpl(serviceApi);
	}
	
	//ִ��service����
	private void doService(Request request,Response response) throws ClassNotFoundException, IOException{
		
		System.out.println("�ͻ���������Ϣ���£�");
		System.out.println(request);
		
		//��clientRequestȡ������service������Ϣ
		//�ͻ�������ӿ���
		String serviceApi = request.getServer();
		String serviceImpl = getServiceImpl(serviceApi);
		if(null == serviceImpl){
			response.setStatusAndValue(Constants.FAILURE,null);
			response.setMsg("Can not find service implements!");
			return;
		}
		
		String methodStr = request.getMethodName();
		Object[] args = request.getParams();
		
		//�����߳�ִ��service����
		ServiceThread service = new ServiceThread(serviceImpl,methodStr,args,response);
		service.start();
	}
	
	private void getResult(Response response,ObjectOutputStream dos) throws InterruptedException, IOException{
		//�õ�service���ý��
		char status = response.getStatus();
		
		Object res = response.getValue();
		//��Ӧ����
		//���ظ�ͻ��˵�״̬��
		dos.writeChar(status);
		System.out.println("������״̬��Ϊ��"+status);
		System.out.println("��������ϢΪ��"+response.getMsg());
		//���ظ�ͻ��˵���Ӧ
		dos.writeObject(res);
		System.out.println("������д����Ӧ������"+res);
	}
	
	public void run(){
		while(true){
			prepare();
			ObjectOutputStream dos = null;
			try {
				dos = new ObjectOutputStream(socket.getOutputStream());
				Response response = new Response();
				//ִ��service
				doService(request,response);
				//��������
				getResult(response,dos);
			}catch(InterruptedException timeout){
				try{
					//��ʱ
					//���ظ�ͻ��˵�״̬��
					dos.writeChar(Constants.TIMEOUT);
					System.out.println("������д��f״̬�롣����");
					//���ظ�ͻ��˵���Ӧ
					dos.writeObject(timeout.getMessage());
				}catch(Exception e){
					System.out.println("��ʱ�������д����Ӧʧ�ܣ�����");
				}
			}catch (IOException e) {
				System.out.println("������д����Ӧʧ�ܣ�����");
			}catch (ClassNotFoundException e) {
				e.printStackTrace();
				try{
					//�쳣
					dos.writeChar(Constants.FAILURE);
				}catch(Exception e1){
				}
			}finally{
				if(null != dos){
					try {
						dos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(null != socket){
					try {
						socket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}
